#include "Collect.h"
#include "Login.h"

#include <QDebug>
#include "c/adc.h"
#include "c/pwm.h"
#include "CApi.h"
#include <QtMath>
#include <mainwindow.h>
#include <QDateTime>
Collect::Collect(QObject *parent):QObject(parent)
{

}

Collect::Collect(QtCharts::QSplineSeries *series,QMutex* collectLock,QtCharts::QChart *chart,int saveMax,QObject *parent):QObject(parent)
{
    this->series=series;
    this->collectLock=collectLock;
    this->saveMax=saveMax;
    this->chart=chart;
    this->myThread=new QThread;
    this->keys.reserve(1000);//设置预内存
    this->values.reserve(1000);//设置预内存
    this->moveToThread(this->myThread);
    this->myThread->start();
    QMetaObject::invokeMethod(this,"init",Qt::AutoConnection);
}

Collect::~Collect()
{
    if(this->myThread!=nullptr)
        delete this->myThread;
}

void Collect::setIsAutomaticMode(bool r)
{
    this->isAutomaticMode=r;
}

void Collect::setIsCloudAndLocalSave(bool isCloud,bool isLocal)
{
    this->isCloudSave=isCloud;
    this->isLocalSave=isLocal;
}

//void Collect::setCurrentCollectNumber(int n)
//{
//    MainWindow::mySqlite->operatorNumber++;
//}

//void Collect::addCurrentCollectNumber()
//{
//    MainWindow::mySqlite->updataOperatorNumber();
//}

void Collect::isStartCollect(int refreshTimeMs,bool r)
{
    this->refreshTimeMs=refreshTimeMs;
    if(r==true)
        this->collectTimer->start(collectIntervalMs);
    else
        this->collectTimer->stop();
}

void Collect::init()
{
    this->collectTimer=new QTimer;
    connect(this->collectTimer,&QTimer::timeout,this,&Collect::collectData);
}

void Collect::clearData()
{
    keys.clear();
    values.clear();
    times.clear();
    counter = 1;
    refreshCounter=1;
    adc_cont=0;
    MainWindow::mySqlite->clearDatas();
}

void Collect::collectData()
{
    static operatorData p;
    int operatorNumber=MainWindow::mySqlite->getOperatorNumber();
    QMutexLocker lock(this->collectLock);
    //读取数据...
    //series存放数据
    int ret = -1;
        ret = adc_read(&myadc);
    //ret=0;
    //myadc.act=2.2;
        if(!ret) {
            p.data=myadc.act;p.time=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");p.operatorNumber=operatorNumber;
            keys.append(counter);
            values.append(p.data);
            times.append(p.time);
            MainWindow::mySqlite->addData(p);
            adc_cont++;
            if(this->isAutomaticMode)//自动模式算法
            {
                if(adc_cont == 30)             //2.5S
                    beng_control(functionFlag::pump_start, 1000000000/600);
                else if(adc_cont == 50)       //2s
                    beng_control(functionFlag::pump_start, 1000000000/800);
                else if(adc_cont == 60)       //1S
                    beng_control(functionFlag::pump_start, 1000000000/1050);
                else if(adc_cont == 150)       //2.5s
                    beng_control(functionFlag::pump_start, 1000000000/500);
                if(adc_cont > 210)
                    adc_cont = 0;
            }
        }


    //keys.append(counter);values.append(qrand() % 3);
    if(refreshCounter*collectIntervalMs>this->refreshTimeMs)
    {
        if(this->isCloudSave || this->isLocalSave)
        {//上传服务器代码和保存本地数据库
            static int count=0;
            static QString str="7E7E;kind:surgery;function:03;collectNumber:%1;data:";
            count++;
            QString factor="%1(%2)";
            if(this->isCloudSave)
            {//上传服务器
                for(int i=0;i<keys.size();i++)
                {
                    if(i==0)
                        str=str.arg(QString::number(operatorNumber));
                    //str=str.append(factor.arg(QString::number(keys[i])).arg(QString::number(values[i])));
                    str=str.append(factor.arg(times[i]).arg(QString::number(values[i])));
                }
            }
            if(count>=10)
            {
                if(this->isCloudSave)
                {
                    QMetaObject::invokeMethod(Login::myTCPClient,"write",Qt::AutoConnection,Q_ARG(QString,str+";"),Q_ARG(bool,false));
                    str="7E7E;kind:surgery;function:03;collectNumber:%1;data:";
                }
                if(this->isLocalSave)
                    QMetaObject::invokeMethod(MainWindow::mySqlite,"insertDataToSql",Qt::AutoConnection);
                count=0;
            }

        }

        for(int i=0;i<keys.size();i++)
        {
            series->append(keys[i],values[i]);
           // qDebug()<<"caojinpeng:"<<ls.key(i)<<"      "<<ls.value(ls.key(i))<<endl;
        }
        //ls.clear();
        keys.clear();values.clear();times.clear();
        if(series->count()>saveMax)
        {
            series->removePoints(0,series->count()-saveMax);
            chart->axisX()->setRange(counter-(saveMax-1), counter);

        }
        refreshCounter=0;
    }
    counter ++;
    refreshCounter=refreshCounter+1;
}

//void Collect::collectData()
//{

//}
